package itbour.onetouchshow.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ys.uilibrary.utils.DisplayUtils;

import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.adapter.base.BaseAdapterHelper;
import itbour.onetouchshow.adapter.base.normal.QuickAdapter;
import itbour.onetouchshow.bean.VideoConditionBean;
import itbour.onetouchshow.utils.UIUtils;

public class DropDownViewPopup extends PopupWindow {
    public ItemSelectListener itemSelectListener;
    private View mPopView;
    private List<VideoConditionBean.Option> mdata;
    //弹出的位置
    private Location location;
    private Context mContext;
    private View leftEmptyView;
    private View rightEmptyView;
    private ListView recyclerView;


    public DropDownViewPopup(Context context, List<VideoConditionBean.Option> data, ItemSelectListener itemSelectListener) {
        this.itemSelectListener = itemSelectListener;
        this.mContext = context;
        this.mdata = data;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mPopView = inflater.inflate(R.layout.view_my_drop_down_expanded, null);
        this.setContentView(mPopView);
        this.setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);//半透明颜色
        initData();
    }

    public void showPopWindow(Location location, View view, ImageView imageView) {
        imageView.setImageResource(R.mipmap.icon_blue_triangle);
        switch (location) {
            case left:
                leftEmptyView.setVisibility(View.GONE);
                rightEmptyView.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams rightParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
                rightEmptyView.setLayoutParams(rightParams);
                break;
            case middle:
                leftEmptyView.setVisibility(View.VISIBLE);
                rightEmptyView.setVisibility(View.VISIBLE);
                break;
            case right:
                rightEmptyView.setVisibility(View.GONE);
                leftEmptyView.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams leftParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
                leftEmptyView.setLayoutParams(leftParams);
                break;
        }

        setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                ObjectAnimator.ofFloat(imageView, View.ROTATION.getName(), 180, 0).start();
                imageView.setImageResource(R.mipmap.icon_white_triangle);
            }
        });

        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            view.getGlobalVisibleRect(visibleFrame);
            int height = view.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            this.setHeight(height);
            this.showAsDropDown(view, DisplayUtils.dip2px(mContext, 50), 0);
        } else {
            this.showAsDropDown(view, DisplayUtils.dip2px(mContext, 50), 0);
        }

//        if (Build.VERSION.SDK_INT < 24) {
//            showAsDropDown(view);
//        } else {
//            int[] array = new int[2];
//            view.getLocationOnScreen(array);
//            showAtLocation(view, Gravity.NO_GRAVITY, array[0], array[1]+ DisplayUtils.dip2px(mContext,50));
//        }


    }

    private void initData() {
        recyclerView = mPopView.findViewById(R.id.recyclerView);
        leftEmptyView = mPopView.findViewById(R.id.v_left_empty);
        leftEmptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rightEmptyView = mPopView.findViewById(R.id.v_right_empty);
        rightEmptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mPopView.findViewById(R.id.ll_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        DropDownAdapter adapter = new DropDownAdapter(UIUtils.getContext(), R.layout.list_item_stand_drop_down);
        adapter.replaceAll(mdata);
        recyclerView.setAdapter(adapter);
    }

    public void setItemSelectListener(ItemSelectListener itemSelectListener) {
        this.itemSelectListener = itemSelectListener;
    }


    public enum Location {
        left,
        middle,
        right
    }

    public interface ItemSelectListener {
        void onSelect(VideoConditionBean.Option option, int position);
    }

    public class DropDownAdapter extends QuickAdapter<VideoConditionBean.Option> {


        public DropDownAdapter(Context context, int layoutResId) {
            super(context, layoutResId);
        }

        @Override
        protected void convert(BaseAdapterHelper helper, VideoConditionBean.Option item, int position) {
            TextView standStatusTV = helper.getView(R.id.cell_stand_status);
            standStatusTV.setText(item.getName());
            if (item.isSelected()) {
                standStatusTV.setTextColor(context.getResources().getColor(R.color.xiumi_text_color));
            } else {
                standStatusTV.setTextColor(Color.WHITE);
            }

            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemSelectListener != null) {
                        for (VideoConditionBean.Option item : getData()) {
                            item.setSelected(false);
                        }
                        getData().get(position).setSelected(true);
                        itemSelectListener.onSelect(item, position);
                        dismiss();
                    }

                }
            });
        }
    }

}